package com.jzsk.variable.center.service.menu;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jzsk.variable.center.common.base.ServiceException;
import com.jzsk.variable.center.dao.zhengxin.entity.SysMenu;
import com.jzsk.variable.center.dao.zhengxin.entity.SysRoleMenuRelation;
import com.jzsk.variable.center.dao.zhengxin.entity.SysUserRoleRelation;
import com.jzsk.variable.center.dao.zhengxin.mapper.SysMenuMapper;
import com.jzsk.variable.center.dao.zhengxin.mapper.SysRoleMenuRelationMapper;
import com.jzsk.variable.center.dao.zhengxin.mapper.SysUserRoleRelationMapper;
import com.jzsk.variable.center.domain.dto.menu.LoadMenuReqDTO;
import com.jzsk.variable.center.domain.dto.menu.LoadMenuRespDTO;
import com.jzsk.variable.center.domain.dto.menu.MenuInfo;
import com.jzsk.variable.center.domain.enums.StatusCodeEnum;
import com.jzsk.variable.center.domain.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    SysUserRoleRelationMapper sysUserRoleRelationMapper;

    @Resource
    SysRoleMenuRelationMapper sysRoleMenuRelationMapper;

    @Resource
    SysMenuMapper sysMenuMapper;

    @Override
    public ResponseVO<LoadMenuRespDTO> loadMenus(LoadMenuReqDTO loadMenuReqDTO) {
        Integer userId = loadMenuReqDTO.getUserId();
        List<SysUserRoleRelation> sysUserRoleRelations = sysUserRoleRelationMapper.selectList(Wrappers.lambdaQuery(new SysUserRoleRelation())
                .eq(SysUserRoleRelation::getUserId, userId));
        if (CollectionUtil.isEmpty(sysUserRoleRelations)) {
            throw new ServiceException(StatusCodeEnum.用户未配置角色);
        }
        List<Integer> roleIds = sysUserRoleRelations.stream().map(SysUserRoleRelation::getRoleId).collect(Collectors.toList());
        List<SysRoleMenuRelation> sysRoleMenuRelations = sysRoleMenuRelationMapper.selectList(Wrappers.lambdaQuery(new SysRoleMenuRelation())
                .in(SysRoleMenuRelation::getRoleId, roleIds));
        if (CollectionUtil.isEmpty(sysRoleMenuRelations)) {
            throw new ServiceException(StatusCodeEnum.角色未配置相应的权限);
        }
        Set<Integer> menuIds = sysRoleMenuRelations.stream().map(SysRoleMenuRelation::getMenuId).collect(Collectors.toSet());
        List<SysMenu> sysMenus = sysMenuMapper.selectBatchIds(menuIds);

        List<MenuInfo> retMenuList = convertMenuInfo(sysMenus);

        LoadMenuRespDTO loadMenuRespDTO = LoadMenuRespDTO.builder().menuList(retMenuList).build();
        return ResponseVO.success(loadMenuRespDTO);
    }

    private List<MenuInfo> convertMenuInfo(List<SysMenu> sysMenus) {
        return sysMenus.stream().map(sysMenu -> MenuInfo.builder()
                .menuId(sysMenu.getId())
                .menuCode(sysMenu.getMenuCode())
                .menuName(sysMenu.getMenuName())
                .menuUrl(sysMenu.getUrl())
                .showed(true)
                .build()).collect(Collectors.toList());
    }

}
